
use "$P_Data_Processed/monthly_station_data_e5.dta", clear

	by year_month Post, sort: gener postal_n_stations = _N
	gener postal_n_others = postal_n_stations-1
	drop if Post==0

	** defining treatment 

	capture drop treat
	gener treat = month_treat_period*treat_group
	label variable treat "Adopter" 

	**
	** drop outlier observations (top and bottom 1%)
	drop if mean_wh_margin<0
	drop if mean_wh_margin>0.2
	drop if mean_price<1.18
	drop if mean_price>1.7
	

	sort StID year_month
	by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0
	sort StID first_treat_month
	by StID: replace first_treat_month = first_treat_month[1]
	replace first_treat_month = 1000 if missing(first_treat_month)
	
	* number of other adopters in your Postal code
	by Post year_month, sort: egen n_compet_adopt = total(treat)
	replace n_compet_adopt = n_compet_adopt - 1 if treat==1	
	
	** generating simple IV - share of adopters in your brand
	by Brand year_month, sort: gener n_brand = _N
	by Brand year_month: egen n_treat = total(treat)

	gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
	replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1

	label variable n_brand "N Brand Stations"
	label variable share_others_treated "Share Brand Adopters"
	label variable postal_n_stations "N Competitors in ZIP"
	label variable n_compet_adopt "N Competitors Adopting"
	
* new instruments


gegen n_nuts3_brand_adopters = total(treat), by(nuts3 Brand year_month)

capture drop this_is_a_station
gener this_is_a_station = 1
gegen n_nuts3_brand_stations = total(this_is_a_station), by(nuts3 Brand year_month)

gener new_iv_nuts3 = (n_treat - n_nuts3_brand_adopters)/(n_brand - n_nuts3_brand_stations)

capture drop nuts2
gener nuts2 = substr(nuts3,1,length(nuts3)-1)


gegen n_nuts2_brand_adopters = total(treat), by(nuts2 Brand year_month)

capture drop this_is_a_station
gener this_is_a_station = 1
gegen n_nuts2_brand_stations = total(this_is_a_station), by(nuts2 Brand year_month)

gener new_iv_nuts2 = (n_treat - n_nuts2_brand_adopters)/(n_brand - n_nuts2_brand_stations)

	
		
merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener station = 1 
gegen n_act_stations = total(station), by(mktid year_month)


gegen n_mkt_brand_adopters = total(treat), by(mktid Brand year_month)

capture drop this_is_a_station
gener this_is_a_station = 1
gegen n_mkt_brand_stations = total(this_is_a_station), by(mktid Brand year_month)

gener new_iv = (n_treat - n_mkt_brand_adopters)/(n_brand - n_mkt_brand_stations)


*by year_month mktid, sort: gener mkt_n_stations = _N
gegen n_mkt_compet_adopt = total(treat), by(year_month mktid)
replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if treat==1

capture drop n_months_since_treat 
sort StID year_month
capture drop age 
by StID: gener age = _n
gener n_months_since_treat = year_month - first_treat_month
replace n_months_since_treat=. if n_months_since_treat<-31

* new clustering
gegen nuts3_id = group(nuts3)
gegen nuts2_id = group(nuts2)

* 1st clustering -> market x time two way clustering a la Chandra and Tappata (2011)
ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = new_iv ) if year<2019, absorb(year_month StID) cluster(mktid year_month)
outreg2 using "$P_Tables/Table_G10", replace tex(frag) dec(3) label  keep(treat)  


ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = new_iv ) if year<2019, absorb(year_month StID) cluster(nuts3)
outreg2 using "$P_Tables/Table_G10", append tex(frag) dec(3) label  keep(treat)  


ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = new_iv ) if year<2019, absorb(year_month StID) cluster(nuts3 year_month)
outreg2 using "$P_Tables/Table_G10", append tex(frag) dec(3) label  keep(treat)  

ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = new_iv ) if year<2019, absorb(year_month StID) cluster(nuts2 )
outreg2 using "$P_Tables/Table_G10", append tex(frag) dec(3) label  keep(treat)  


ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = new_iv ) if year<2019, absorb(year_month StID) cluster(nuts2 year_month)
outreg2 using "$P_Tables/Table_G10", append tex(frag) dec(3) label  keep(treat)  
